package org.xdj.blue.java.netty.client;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;

public class NettyClientSocket implements Runnable {
	private String ip;
	private int port;
	
	public NettyClientSocket(String ip,int port) {
		this.ip = ip;
		this.port = port;
	}
	
	public void run() {
		
		EventLoopGroup work = new NioEventLoopGroup();
		Bootstrap client = new Bootstrap();
		try {
			client.group(work).channel(NioSocketChannel.class).handler(new ClientChannelInitializer());
			ChannelFuture f =client.connect(ip, port).sync();
			f.channel().closeFuture().sync();
		} catch (Exception e) {
			// TODO: handle exception
		}
		finally{
			work.shutdownGracefully();
		}
	}

}
